Ext.onReady(function() {
    Ext.SSL_SECURE_URL = "../../ext-3.2.1/resources/images/default/s.gif";
    Ext.BLANK_IMAGE_URL = "../../ext-3.2.1/resources/images/default/s.gif";
//    Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
    //Ext.util.CSS.swapStyleSheet('theme', currentTheme);
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'side';
    var selectTreeNode;
    var tb = new Ext.Toolbar('north-div');//创建一个工具条
    var addButton = new Ext.Toolbar.Button({
        icon    : "../../ext-3.2.1/examples/shared/icons/fam/add.png",
        id      : 'addButton',
        cls: 'x-btn-text-icon',
        text    : '添加',
//        disabled:true,
        tooltip : '添加系统菜单',
        handler : onAddClick
    });
    var modButton = new Ext.Toolbar.Button({
        icon    : "../../ext-3.2.1/examples/shared/icons/fam/edit.png",
        id      : 'modButton',
        cls: 'x-btn-text-icon',
        text    : '修改',
//        disabled:true,
        tooltip : '修改选中系统菜单。',
        handler : onModClick
    });
    var delButton = new Ext.Toolbar.Button({
        icon    : "../../ext-3.2.1/examples/shared/icons/fam/del.png",
        id      : 'delButton',
        cls: 'x-btn-text-icon',
        text    : '删除',
//        disabled:true,
        tooltip : '删除已选中系统菜单。',
        handler : onDelClick
    });

    tb.add(addButton, '-', modButton, '-', delButton);

    new Ext.Viewport({
        layout:'border',
        items:[
            {
                border:true,
                region:'north',
                layout:'fit',
                contentEl:'north-div',
                bbar:tb,
                margins:'0 0 0 0',
                height: 28
            },
            {
                region:'center',
                deferredRender:true,
                layout:'fit',
                margins:'0 0 0 0',
                autoScroll:true,
                items:[
                    {
                        border:false,
                        iconCls:'nav',
                        autoScroll:true,
                        contentEl:'center-div'
                    }
                ]
            }

        ]
    });

    var tree;
    var root;
    if (!tree) {
        var Tree = Ext.tree;
        tree = new Tree.TreePanel(
        {
            el:'center-div',
            layout:'fit',
            autoScroll:true,
            animate:true,
            border:false,
            enableDD:true,
            containerScroll: true,
            rootVisible:true,
            draggable:false,
            lines:true,
            loader: new Tree.TreeLoader({
                dataUrl:'../../menu/buildMenuJSON.action'
            })
        }
                );
        root = new Tree.AsyncTreeNode({
            text: '系统功能菜单',
            draggable:false,
            id:'0'
        });
        tree.setRootNode(root);

        tree.on('click', function (node, e) {
            e.stopEvent();
            selectTreeNode = node;
            if (node.isLeaf()) {
                //                centerTabPanel.loadPage(node.attributes.href, node.id, node.text);
            } else {
                //                node.toggle();
            }
        });
        tree.on('dblclick', function (node, e) {
            e.stopEvent();
            selectTreeNode = node;
            if (!modButton.disabled) onModClick(modButton);
        });

    }
    tree.render();
    root.expand(false, true);
    tree.on("nodedragover", function(e) {
        var n = e.target;
        if (n.leaf) {
            n.leaf = false;
        }
        return true;
    });
    tree.on("nodedrop", function(e) {
        var dropNode = e.dropNode;
        var parent = dropNode.parentNode;
        var indexof = parent.indexOf(dropNode);
        Ext.MessageBox.wait('正在处理，请稍等...');
        Ext.Ajax.request({
            url:  '../../menu/dropMenu.action',
            failure: function(result, request) {
                Ext.MessageBox.hide();
                Ext.Msg.show({
                    title: '错误',
                    msg: '<span style="text-align:center;width:250px;margin:5 0 5 0;">' + '拖动失败！' + '</span>',
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.WARNING
                });
            },
            success: function(result, request) {
                Ext.MessageBox.hide();
                Ext.Msg.show({
                    title: '成功',
                    msg: '<span style="text-align:center;width:250px;margin:5 0 5 0;">' + '拖动成功！' + '</span>',
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.INFO
                });

//                root.reload()
                //                root.expand(true, false);
            },
            params: { dropNodeID: dropNode.id, parentID: parent.id,indexof:indexof}
        });
    });

    function onDelClick(item) {
        var n = tree.getSelectionModel().getSelectedNode();
        if (!n) {
            Ext.Msg.alert('提示', "未选中任何一条记录，请选择要删除的菜单！");
            return false;
        } else if (n.id == 0) {
            Ext.Msg.alert('提示', "注意：最上层的一个菜单，不允许用户删除！");
            return false;
        }
        else {
            selectTreeNode = n;
            if (n.isLeaf()) {
                Ext.MessageBox.confirm('提示框', '请确认要删除系统菜单吗?按是将删除选中系统菜单！', function doDel(btn) {
                    if (btn == 'yes') {
                        Ext.MessageBox.wait('正在处理，请稍等...');
                        Ext.Ajax.request({
                            url:  '../../menu/deleteMenu.action',
                            failure: function(result, request) {
                                Ext.MessageBox.hide();
                                Ext.Msg.show({
                                    title: '错误',
                                    msg: '<span style="text-align:center;width:250px;margin:5 0 5 0;">' + '删除失败！' + '</span>',
                                    buttons: Ext.MessageBox.OK,
                                    icon: Ext.MessageBox.WARNING
                                });
                            },
                            success: function(result, request) {
                                Ext.MessageBox.hide();
                                Ext.Msg.show({
                                    title: '成功',
                                    msg: '<span style="text-align:center;width:250px;margin:5 0 5 0;">' + '删除成功！' + '</span>',
                                    buttons: Ext.MessageBox.OK,
                                    icon: Ext.MessageBox.INFO
                                });
                                n.parentNode.removeChild(n);
//                                root.reload()
                                //                                root.expand(true, false);
                            },
                            params: { deleteNodeID: selectTreeNode.id}
                        });
                    }
                })
            }
            else {
                Ext.Msg.alert('提示', "选择的菜单有下级菜单，请先删除下级菜单！");
                return false;
            }
        }
    }

    var modformPanel ;
    var modwin;
    function onModClick(item) {
        var n = tree.getSelectionModel().getSelectedNode();
        if (!n) {
            Ext.Msg.alert('提示', "未选中任何一条记录，请选择要修改的菜单！");
            return false;
        }
        if (n.id == 0) {
            Ext.Msg.alert('提示', "注意：最上层的一个菜单，不允许用户修改！");
            return false;
        }
        selectTreeNode = n;
        var menuname,url,title,menuid;
        menuid = new Ext.form.Hidden({
            name:'menuid',
            id:'menuid',
            value: selectTreeNode.id
        });
        menuname = new Ext.form.TextField({
            fieldLabel: '菜单名称',
            id:'modmenuname',
            name: 'menuname',
            width:150,
            maxLength:16,
            maxLengthText:"最大长度16",
            value: selectTreeNode.text,
            allowBlank:false
        });

        url = new Ext.form.TextField({
            fieldLabel: '链接地址',
            width:280,
            maxLength:200,
            maxLengthText:"最大长度200",
            id:'modurl',
            value: selectTreeNode.attributes.href,
            name: 'url'
        });

        title = new Ext.form.TextField({
            fieldLabel: '菜单说明',
            width:280,
            maxLength:25,
            maxLengthText:"最大长度25",
            value: selectTreeNode.attributes.qtip,
            id:'modtitle',
            name: 'title'
        });
        if (modformPanel != null) {
            modformPanel.destroy(true);
            modformPanel = null;
        }
        if (!modformPanel) {
            modformPanel = new Ext.FormPanel({
                labelAlign:"right",
                labelWidth: 125,
                defaults: {width: 450},
                url: '../../menu/updateMenu.action',
                frame:false,
                bodyStyle:'padding:60px 5px 0',
                items: [ menuid,{
                    layout:'column',
                    border:false,
                    labelSeparator:'：',
                    items:[  {
                        columnWidth:1,
                        layout: 'form',
                        border:false,
                        items: [menuname]
                    } ,{
                        columnWidth:.42,
                        layout: 'form',
                        border:false,
                        items: [{
                            xtype:'checkbox',
                            fieldLabel: '菜单功能',
                            boxLabel:'浏览',
                            inputValue:'1',
                            checked:true
                        }]
                    },{
                        columnWidth:.13,
                        layout: 'form',
                        labelWidth:0,
                        labelSeparator:'',
                        hideLabels:true,
                        border:false,
                        items: [{
                            xtype:'checkbox',
                            fieldLabel: '',
                            boxLabel:'添加',
                            name: 'permission1',
                            id: 'permission1',
                            inputValue:'1',
                            checked:selectTreeNode.attributes.permission1 == '1'
                        }]
                    },{
                        columnWidth:.13,
                        layout: 'form',
                        labelWidth:0,
                        labelSeparator:'',
                        hideLabels:true,
                        border:false,
                        items: [{
                            xtype:'checkbox',
                            fieldLabel: '',
                            boxLabel:'修改',
                            name: 'permission2',
                            id: 'permission2',
                            inputValue:'1',
                            checked:selectTreeNode.attributes.permission2 == '1'
                        }]
                    },{
                        columnWidth:.13,
                        layout: 'form',
                        labelWidth:0,
                        labelSeparator:'',
                        hideLabels:true,
                        border:false,
                        items: [{
                            xtype:'checkbox',
                            fieldLabel: '',
                            boxLabel:'删除',
                            name: 'permission3',
                            id: 'permission3',
                            inputValue:'1' ,
                            checked:selectTreeNode.attributes.permission3 == '1'
                        }]
                    },{
                        columnWidth:.19,
                        layout: 'form',
                        labelWidth:0,
                        labelSeparator:'',
                        hideLabels:true,
                        border:false,
                        items: [{
                            xtype:'checkbox',
                            fieldLabel: '',
                            boxLabel:'其它',
                            name: 'permission4',
                            id: 'permission4',
                            inputValue:'1',
                            checked:selectTreeNode.attributes.permission4 == '1'
                        }]
                    } ,{
                        columnWidth:1,
                        layout: 'form',
                        border:false,
                        items: [url]
                    },{
                        columnWidth:1,
                        layout: 'form',
                        border:false,
                        items: [title]
                    }
                    ]
                }
                ]
            });
        }
        if (modwin != null) {
            modwin.destroy(true);
            modwin = null;
        }
        if (!modwin) {
            modwin = new Ext.Window({
                id : 'formModWin',
                title: '修改系统菜单',
                width: 500,
                height:420,
                minWidth: 200,
                minHeight: 200,
                xtype:'window',
                maximizable : false,
                closeAction : 'hide',
                layout: 'fit',
                plain:false,
                closable:true,
                modal:false,
                bodyStyle:'padding:5px;',
                buttonAlign:'center',
                border:false,
                items: modformPanel,
                buttons: [{
                    handler: function() {
                        if (modformPanel.getForm().isValid()) {
                            modformPanel.getForm().submit({
                                waitMsg: '正在处理，请稍等...',
                                reset: true,
                                failure: function(form, action) {
                                    Ext.MessageBox.hide();
                                    Ext.Msg.show({
                                        title: '错误',
                                        msg: '<span style="text-align:center;width:250px;margin:5 0 5 0;">' + action.result.msg + '</span>',
                                        buttons: Ext.MessageBox.OK,
                                        icon: Ext.MessageBox.WARNING
                                    });
                                },
                                success: function(form, action) {
                                    Ext.MessageBox.hide();
                                    Ext.Msg.show({
                                        title: '成功',
                                        msg: '<span style="text-align:center;width:250px;margin:5 0 5 0;">' + action.result.msg + '</span>',
                                        buttons: Ext.MessageBox.OK,
                                        icon: Ext.MessageBox.INFO
                                    });

                                    modwin.hide();
                                    root.reload()
//                                    root.expand(true, false);
                                },
                                method:'POST'
                            });
                        }
                    },
                    text: '保存'
                },{
                    handler: function() {
                        modwin.hide();
                    },
                    text: '取消'
                }]
            });
        }
        modwin.show();
    }

    var formPanel ;
    var win;
    function onAddClick(item) {
        var n = tree.getSelectionModel().getSelectedNode();
        if (!n) {
            Ext.Msg.alert('提示', "未选中任何一条记录，请先选定上一级菜单！");
            return false;
        }
        selectTreeNode = n;
        var menuname,url,title,menupid;
        menupid = new Ext.form.Hidden({
            name:'menupid',
            id:'menupid',
            value: selectTreeNode.id
        });

        menuname = new Ext.form.TextField({
            fieldLabel: '菜单名称',
            id:'menuname',
            name: 'menuname',
            width:150,
            maxLength:16,
            maxLengthText:"最大长度16",
            allowBlank:false
        });

        url = new Ext.form.TextField({
            fieldLabel: '链接地址',
            width:280,
            maxLength:200,
            maxLengthText:"最大长度200",
            id:'url',
            name: 'url'
        });

        title = new Ext.form.TextField({
            fieldLabel: '菜单说明',
            width:280,
            maxLength:25,
            maxLengthText:"最大长度25",
            id:'title',
            name: 'title'
        });
        if (!formPanel) {
            formPanel = new Ext.FormPanel({
                labelAlign:"right",
                labelWidth: 125,
                defaults: {width: 450},
                url: '../../menu/createMenu.action',
                frame:false,
                bodyStyle:'padding:60px 5px 0',
                items: [ menupid,{
                    layout:'column',
                    border:false,
                    labelSeparator:'：',
                    items:[  {
                        columnWidth:1,
                        layout: 'form',
                        border:false,
                        items: [menuname]
                    }  , {
                        columnWidth:.42,
                        layout: 'form',
                        border:false,
                        items: [{
                            xtype:'checkbox',
                            fieldLabel: '菜单功能',
                            boxLabel:'浏览',
                            checked:true
                        }]
                    },{
                        columnWidth:.13,
                        layout: 'form',
                        labelWidth:0,
                        labelSeparator:'',
                        hideLabels:true,
                        border:false,
                        items: [{
                            xtype:'checkbox',
                            fieldLabel: '',
                            boxLabel:'添加',
                            name: 'permission1',
                            inputValue:'1'
                        }]
                    },{
                        columnWidth:.13,
                        layout: 'form',
                        labelWidth:0,
                        labelSeparator:'',
                        hideLabels:true,
                        border:false,
                        items: [{
                            xtype:'checkbox',
                            fieldLabel: '',
                            boxLabel:'修改',
                            name: 'permission2',
                            inputValue:'1'
                        }]
                    },{
                        columnWidth:.13,
                        layout: 'form',
                        labelWidth:0,
                        labelSeparator:'',
                        hideLabels:true,
                        border:false,
                        items: [{
                            xtype:'checkbox',
                            fieldLabel: '',
                            boxLabel:'删除',
                            name: 'permission3',
                            inputValue:'1'
                        }]
                    },{
                        columnWidth:.19,
                        layout: 'form',
                        labelWidth:0,
                        labelSeparator:'',
                        hideLabels:true,
                        border:false,
                        items: [{
                            xtype:'checkbox',
                            fieldLabel: '',
                            boxLabel:'其它',
                            name: 'permission4',
                            inputValue:'1'
                        }]
                    },{
                        columnWidth:1,
                        layout: 'form',
                        border:false,
                        items: [url]
                    },{
                        columnWidth:1,
                        layout: 'form',
                        border:false,
                        items: [title]
                    }
                    ]
                }
                ]
            });
        } else {
            formPanel.getForm().reset();
            formPanel.findById("menupid").setValue(selectTreeNode.id);
        }
        if (!win) {
            win = new Ext.Window({
                id : 'formWin',
                title: '添加系统菜单',
                width: 500,
                height:420,
                minWidth: 200,
                minHeight: 200,
                xtype:'window',
                maximizable : false,
                closeAction : 'hide',
                layout: 'fit',
                plain:false,
                closable:true,
                modal:false,
                border:false,
                bodyStyle:'padding:5px;',
                buttonAlign:'center',
                items: formPanel,
                buttons: [{
                    handler: function() {
                        if (formPanel.getForm().isValid()) {
                            formPanel.getForm().submit({
                                waitMsg: '正在处理，请稍等...',
                                reset: true,
                                failure: function(form, action) {
                                    Ext.MessageBox.hide();
                                    Ext.Msg.show({
                                        title: '错误',
                                        msg: '<span style="text-align:center;width:250px;margin:5 0 5 0;">' + action.result.msg + '</span>',
                                        buttons: Ext.MessageBox.OK,
                                        icon: Ext.MessageBox.WARNING
                                    });
                                },
                                success: function(form, action) {
                                    Ext.MessageBox.hide();
                                    Ext.Msg.show({
                                        title: '成功',
                                        msg: '<span style="text-align:center;width:250px;margin:5 0 5 0;">' + action.result.msg + '</span>',
                                        buttons: Ext.MessageBox.OK,
                                        icon: Ext.MessageBox.INFO
                                    });

                                    win.hide();
                                    root.reload()
//                                    root.expand(true, false);
                                },
                                method:'POST'
                            });
                        }
                    },
                    text: '保存'
                },{
                    handler: function() {
                        win.hide();
                    },
                    text: '取消'
                }]
            });
        }
        win.show();
    }

})